using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBase<GameManager>, ISaveable
{
    private SavePoint[] savePointArray;


    private void Start()
    {
        savePointArray = FindObjectsOfType<SavePoint>();
    }
    public void SavePointAnimatorRelease()
    {
        for (int i = 0; i < savePointArray.Length; i++)
        {
            savePointArray[i].isActive = false;
            savePointArray[i].animator.SetBool("IsActive", false);
        }
    }

    public void Save(ref GameData data)
    {
        data.savePointDic.Clear();
        for (int i = 0; i < savePointArray.Length; i++)
        {
            data.savePointDic.Add(savePointArray[i].GUID, savePointArray[i].isActive);
        }
        data.closeSavePointID = FindCloseCheckPoint().GUID;
    }

    public void Load(GameData data)
    {
        foreach (KeyValuePair<string, bool> pair in data.savePointDic)
        {
            foreach (SavePoint point in savePointArray)
            {
                if (pair.Key == point.GUID && pair.Value == true)
                {
                    point.SetActive();
                }
            }
        }
        foreach (SavePoint savePoint in savePointArray)
        {
            if (savePoint.GUID == data.closeSavePointID)
            {
                PlayerManager.Instance.player.transform.position = savePoint.transform.position;
            }
        }
    }
    public SavePoint FindCloseCheckPoint()
    {
        float path = int.MaxValue;
        SavePoint savepoint = null;
        foreach (SavePoint point in savePointArray)
        {
            float distance = Vector2.Distance(PlayerManager.Instance.player.transform.position, point.transform.position);
            if (distance < path)
            {
                path = distance;
                savepoint = point;
            }
        }
        return savepoint;
    }
    public void PauseGame(bool pause)
    {
        if (pause)
            Time.timeScale = 0;
        else
            Time.timeScale = 1;
    }
}
